package com.ruoyi.api.controller;

import com.ruoyi.api.vo.ImServiceVO;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.vim.domain.ImService;
import com.ruoyi.vim.service.IImServiceService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客服Controller
 *
 * @author wgp
 * @since 2022-12-11
 */
@RestController
@RequestMapping("/api/im/service")
public class ImServiceController extends BaseController<ImService> {
    @Autowired
    private IImServiceService imServiceService;

    /**
     * 列表
     * @param imService 实体
     */
    @GetMapping
    public TableDataInfo list(ImServiceVO imService) {
        QueryWrapper<ImService> queryWrapper = getQueryWrapper(imService);
        IPage<ImService> iPage = imServiceService.page(new Page<ImService>(imService.getPageNum(), imService.getPageSize()), queryWrapper);
        return getDataTable(iPage);
    }

    /**
     * 新增客服
     */
    @PostMapping
    public AjaxResult add(@RequestBody ImServiceVO vo) {
        return toAjax(imServiceService.add(vo));
    }

    /**
     * 修改客服
     */
    @PutMapping
    public AjaxResult edit(@RequestBody ImService imService) {
        return toAjax(imServiceService.edit(imService));
    }

    /**
     * 删除客服
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(imServiceService.removeById(id));
    }

    /**
     * 查询客服详情信息
     */
    @GetMapping("/{id}")
    public AjaxResult queryServiceDetail(@PathVariable Long id) {
        return AjaxResult.success(imServiceService.getServiceDetailInfo(id));
    }

    private QueryWrapper<ImService> getQueryWrapper(ImService imService) {
        QueryWrapper<ImService> queryWrapper = new QueryWrapper<>();
        if (imService.getUserId() != null) {
            queryWrapper.eq("user_id", imService.getUserId());
        }
        if (StringUtils.isNotEmpty(imService.getStatus())) {
            queryWrapper.eq("status", imService.getStatus());
        }
        if (StringUtils.isNotEmpty(imService.getServiceType())) {
            queryWrapper.eq("service_type", imService.getServiceType());
        }
        return queryWrapper;
    }
}
